﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using System.Data.SqlClient;
using System.Web.Profile;
using System.Web.Security;

namespace YQBlog.Models
{
    /// <summary>
    /// 账号相关数据操作实现
    /// </summary>
    public partial class ServiceImpl : IServices
    {
        /// <summary>
        /// 获取用户Profile
        /// </summary>
        public UserProfileModel GetUserProfile(string user)
        {
            UserProfileModel userprofile = new UserProfileModel();
            try
            {
                ProfileBase objProfile = System.Web.Profile.ProfileBase.Create(user);

                userprofile.NickName = GetUserProfileItem(objProfile, "nickname");
                userprofile.Signature = GetUserProfileItem(objProfile, "signature");
                userprofile.Intro = GetUserProfileItem(objProfile, "intro");
                userprofile.Gender = GetUserProfileItem(objProfile, "gender", "1");
                userprofile.Birth = GetUserProfileItem(objProfile, "birth");
                userprofile.Location = GetUserProfileItem(objProfile, "location");
                userprofile.Website = GetUserProfileItem(objProfile, "website");
                userprofile.QQ = GetUserProfileItem(objProfile, "qq");
                userprofile.Sina = GetUserProfileItem(objProfile, "sina");
                userprofile.Facebook = GetUserProfileItem(objProfile, "facebook");
                userprofile.Twitter = GetUserProfileItem(objProfile, "twitter");
                userprofile.Medals = GetUserProfileItem(objProfile, "medals");
                userprofile.Phone = GetUserProfileItem(objProfile, "phone");

                string email = GetUserProfileItem(objProfile, "email");
                if (string.IsNullOrEmpty(email))
                {
                    MembershipUser currentUser = Membership.GetUser(user.Trim(), false /* userIsOnline */);
                    userprofile.Email = currentUser.Email;
                }
                else
                {
                    userprofile.Email = email;
                }
                userprofile.IsSendEmail = GetUserProfileItem(objProfile, "isSendEmail", "1");
            }
            catch
            { }
            return userprofile;
        }

        /// <summary>
        /// 获取用户ProfileItem
        /// </summary>
        public string GetUserProfileItem(ProfileBase objProfile, string key, string defaultvalue = "")
        {
            string value = objProfile.GetPropertyValue(key).ToString();
            return string.IsNullOrEmpty(value) ? defaultvalue : value;
        }
    }

}